package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.constants.SystemConstants;
import com.doubi.mapper.ProvinceMapper;
import com.doubi.model.sys.TProvince;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProvinceService extends ServiceImpl<ProvinceMapper, TProvince> {
    @Autowired
    private ProvinceMapper provinceMapper;
    /**
     * 功能说明：查询省份分页信息
     * @param page     page
     * @param province bean
     * @return page
     */
    public PageVo<TProvince> getPage(PageVo<TProvince> page, TProvince province) {
        QueryWrapper<TProvince> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(province.getProvinceName())){
            wrapper.like("province_name",province.getProvinceName());
        }
        wrapper.orderByAsc("sort_num");
        provinceMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 功能说明：查询省份单个信息
     * @param oid oid 数据唯一标识
     * @return bean
     */
    public TProvince getBean(String oid) {
        return super.getById(oid);
    }

    /**
     * 功能说明：查询省份列表信息
     * @param province bean
     * @return list
     */
    public List<TProvince> getList(TProvince province) {
        QueryWrapper<TProvince> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(province.getProvinceName())){
            wrapper.like("province_name",province.getProvinceName());
        }
        if(DataUtil.isNotEmpty(province.getProvinceCode())){
            wrapper.like("province_code",province.getProvinceCode());
        }
        wrapper.orderByAsc("sort_num");
        return provinceMapper.selectList(wrapper);
    }

    /**
     * 功能说明：省份 新增、修改
     * @param province bean
     * @return AppModel
     */
    public AppModel saveOrUpdateBean(TProvince province) {
        //修改操作
        if (DataUtil.isNotEmpty(province.getProvinceOid())) {
            TProvince bean = super.getById(province.getProvinceOid());
            if (DataUtil.isEmpty(bean)) {
                return new AppModel(-1, "参数错误！");
            }
            //进行 数据的拷贝
            bean.setProvinceCode(province.getProvinceCode());
            bean.setProvinceName(province.getProvinceName());
            bean.setStatus(province.getStatus());
            bean.setSortNum(province.getSortNum());
            provinceMapper.updateCityByProvince(province.getProvinceOid(), province.getProvinceCode());
            provinceMapper.updateById(bean);
        } else {
            //进行初始化操作

            provinceMapper.insert(province);
        }
        return AppModel.success();
    }

    /**
     * 功能说明：省份 删除
     * @param province bean
     * @return AppModel
     */
    public AppModel delBean(TProvince province) {
        province = super.getById(province.getProvinceOid());
        if (DataUtil.isEmpty(province)) {
            return new AppModel(-1, "参数错误！");
        } else {
            provinceMapper.delAreaByProvince(province.getProvinceOid());
            provinceMapper.delCityByProvince(province.getProvinceOid());
            provinceMapper.deleteById(province.getProvinceOid());
            return new AppModel(0, "删除成功！");
        }
    }

    /**
     * 功能说明：省份状态修改
     * @param province bean
     * @return model
     */
    public AppModel updateStatus(TProvince province) {
        province = super.getById(province.getProvinceOid());
        if (DataUtil.isEmpty(province)) {
            return new AppModel(-1, "参数错误！");
        } else {
            if (province.getStatus().equals(SystemConstants.STATUS.STATUS_1)) {
                province.setStatus(SystemConstants.STATUS.STATUS_0);
            } else {
                province.setStatus(SystemConstants.STATUS.STATUS_1);
            }
            provinceMapper.updateById(province);
            return new AppModel(0, "修改成功！");
        }
    }
}
